﻿using CSharp.Libs.Database;
using CSharp.Libs.WPF;
using CSharp.Libs.WPF.UserControls.SmartMediaPlayer;
using CSharp.Libs.WPF.UserControls.SmartMediaPlayer.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;


namespace CSharp.Libs.WPF.UserControls.SmarMediaPlayer.ViewModel
{
	public delegate void FinishedPlayingCurrentFileEventDelegate(object sender, PlayerEventArgs e);
	public class PlayerEventArgs : EventArgs
	{
		public string CurrentFile { get; set; }
	}

	public class PlayerViewModel : ViewModelBase
	{
		#region declarations
		//events
		public event FinishedPlayingCurrentFileEventDelegate FinishedPlayingCurrentFile;
		internal void ThrowFinishedPlayingCurrentFileEvent(
			PlayerEventArgs e)
		{
			if (FinishedPlayingCurrentFile != null)
			{
				FinishedPlayingCurrentFile(this, e);
			}
		}
		#endregion



		#region constructor
		public PlayerViewModel()
		{
			//Example for building an Uri
			//MediaSource = new Uri(@"file://o:\Music\Disturbed - Stricken.avi");


		}
		#endregion



		#region private functions
		#endregion



		#region methods
		public void Shutdown()
		{
		}

		public void PlayFile(
			PlaylistItem pliCurrentItem)
		{
			CurrentItem = pliCurrentItem;
			PlayFile();
		}
		public void PlayFile()
		{
			//check if file exists, then Play
			if (!string.IsNullOrEmpty(CurrentFileName) &&
				File.Exists(CurrentFileName))
			{
				this.MediaSource = new Uri(string.Concat(@"file://", CurrentFileName));
			}
		}
		#endregion



		#region properties
		private PlaylistItem _pliCurrentItem;
		public PlaylistItem CurrentItem
		{
			get
			{
				return _pliCurrentItem;
			}
			set
			{
				_pliCurrentItem = value;
				OnPropertyChanged("CurrentItem");
				OnPropertyChanged("CurrentFileName");
			}
		}

		public string CurrentFileName
		{
			get
			{
				return _pliCurrentItem.Filename;
			}
		}

		private Uri _uriMediaSource;
		public Uri MediaSource
		{
			get { return _uriMediaSource; }
			set
			{
				_uriMediaSource = value;
				OnPropertyChanged("MediaSource");
			}
		}
		#endregion
	}
}
